import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import facade.FormFacade;
import form.Form;

import builder.CoreAssetFormBuilder;
import builder.FormBuilder;
import builder.FormDirector;
import builder.RetrieveDecorator;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;


public class MenuUI extends JPanel implements ChangeListener{

	private AssetRegistryFrame arFrame;
	JTabbedPane accounts = new JTabbedPane(JTabbedPane.TOP);
	JTabbedPane assets = new JTabbedPane(JTabbedPane.TOP);
	JTabbedPane logs = new JTabbedPane(JTabbedPane.TOP);
	LoginUI login;
	String globalusername = LoginUI.GlobalUsername.getText().trim();
	ViewAssetUI viewAssetUI;
	EditAssetUI editAssetUI;
	AccountManagerUI accountmanager;
	String usertype =  null;
	JLabel lblHello;
	
	/**
	 * Create the panel.
	 */
	public MenuUI(AssetRegistryFrame arFrame) {
		this.arFrame = arFrame;
		setLayout(null);
		
		// CHECK USERTYPE
		Connection con = DBConnector.getConnection();
		String query = "SELECT usertype FROM useraccounts WHERE username = '"+globalusername+"'";
		usertype = DBConnector.executeQueryWithSingleValue(con, query);
				
		// ACCOUNTS
		accounts.setBounds(129, 0, 650, 600);
		add(accounts);
		
		if(usertype.compareTo("ADM") == 0){
			accountmanager = new AccountManagerUI(arFrame);
			accounts.addTab("Account Manager", null, accountmanager, null);
			
		}
		
		accounts.addChangeListener(this);

		EditAccountUI editaccount = new EditAccountUI();
		accounts.addTab("Edit Account", null, editaccount, null);
		
		
		// ASSETS
		assets.setBounds(129, 0, 650, 600);
		add(assets);
		assets.setVisible(false);
		//assets.hide();
		
		//JTabbedPane assetTab = new JTabbedPane(JTabbedPane.TOP);
		
		/**
		 *  Use this for the facade but restriction of the users is still not implemented
		 *  Event handlers for buttons not yet implemented
		 */
		
        /*
        FormFacade form = new FormFacade();
        Form[] formlist = form.create(0);
        for(int i = 0; i < formlist.length; i++){
        	assets.addTab("Asset", null, formlist[i], null);
        }
        this.add(assets);
		*/
        
		if((usertype.compareTo("MAN") == 0) || (usertype.compareTo("MEM") == 0)){
			if(usertype.compareTo("MAN") == 0){
				AddAssetUI addasset = new AddAssetUI();
				assets.addTab("Add Asset", null, addasset, null);
				
				editAssetUI= new EditAssetUI();
				assets.addTab("Edit Assets", null, editAssetUI, null);
				
				JButton btnLogs = new JButton("Logs");
				btnLogs.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						btnLogsActionPerformed(e);
					}
				});
				btnLogs.setBounds(20, 260, 89, 23);
				add(btnLogs);
			}
			
			viewAssetUI = new ViewAssetUI();
			assets.addTab("View Assets", null, viewAssetUI, null);
			
			assets.addChangeListener(this);
			
			JButton btnAssets = new JButton("Assets");
			btnAssets.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnAssetsActionPerformed(e);
				}
			});
			btnAssets.setBounds(20, 201, 89, 23);
			add(btnAssets);
		}
		
		
		
		// LOGS
		logs.setBounds(129, 0,  650, 600);
		add(logs);
		logs.setVisible(false);
		//logs.hide();
		
		if(usertype.compareTo("MAN") == 0){
			ViewLogsUI viewlog = new ViewLogsUI();
			logs.addTab("View Logs", null, viewlog, null);
		}
		
		JButton btnAccounts = new JButton("Accounts");
		btnAccounts.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnAccountsActionPerformed(arg0);
			}
		});
		btnAccounts.setBounds(20, 142, 89, 23);
		add(btnAccounts);
		
		JButton btnLogout = new JButton("Logout");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLogoutActionPerformed(e);
			}
		});
		
		if(usertype.compareTo("MAN") == 0)
			btnLogout.setBounds(20, 315, 89, 23);
		else if(usertype.compareTo("NEW") == 0 || usertype.compareTo("ADM") == 0)
			btnLogout.setBounds(20, 201, 89, 23);
		else
			btnLogout.setBounds(20, 260, 89, 23);
		add(btnLogout);
		
		lblHello = new JLabel("Hello " + globalusername + "!");
		lblHello.setBounds(10, 50, 109, 14);
		add(lblHello);

	}
	
	private void btnAccountsActionPerformed(ActionEvent arg0) {
		/*
		assets.hide();
		accounts.show();
		logs.hide();
		*/
		assets.setVisible(false);
		accounts.setVisible(true);
		logs.setVisible(false);
	}
	
	private void btnAssetsActionPerformed(ActionEvent arg0) {
		/*
		assets.show();
		accounts.hide();
		logs.hide();
		*/
		assets.setVisible(true);
		accounts.setVisible(false);
		logs.setVisible(false);
	}
	
	private void btnLogsActionPerformed(ActionEvent arg0) {
		assets.setVisible(false);
		accounts.setVisible(false);
		//assets.hide();
		//accounts.hide();
		logs.removeTabAt(0);
		ViewLogsUI viewlog = new ViewLogsUI();
		logs.addTab("View Logs", null, viewlog, null);
		//logs.show();
		logs.setVisible(true);
		
	}
	
	private void btnLogoutActionPerformed(ActionEvent arg0) {
		arFrame.changePanel(new LoginUI(arFrame));
	}
	

	public void stateChanged(ChangeEvent e) {
		 globalusername = LoginUI.GlobalUsername.getText().trim();
         
		 if(assets.getSelectedIndex() == 2)
         	viewAssetUI.setAssetNames();
         else if(assets.getSelectedIndex() == 1)
        	 editAssetUI.setAssetNames();
         
         if(usertype.compareTo("ADM") == 0)
        	 if(accounts.getSelectedIndex() == 0)
        		 accountmanager.setAccounts();
        
     }
	

	
}
